import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:date_format/date_format.dart';
import 'package:tree_hole/letters/LetterPaperPage.dart';

class ToMyselfPage extends StatelessWidget{
  final String token;
  ToMyselfPage(this.token);
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          child: Text('写给自己',
            style: TextStyle(
                color: Colors.black,
                letterSpacing: 5
            ),
          ),
        ),
        centerTitle:true,
        backgroundColor: Colors.white,
        //titleSpacing: 20,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      body: Container(
        width: double.infinity,
        child: _TimeButton(this.token),
        decoration: BoxDecoration(
            image: new DecorationImage(
                fit: BoxFit.cover,
                image: new AssetImage('images/timeCapsule.jpg'))),
      ),
    );
  }
}

class _TimeButton extends StatefulWidget {
  final String token;
  _TimeButton(this.token);
  _TimeButtonState createState() => new _TimeButtonState();
}
class _TimeButtonState extends State<_TimeButton>{
  String _date = formatDate(DateTime.now(), [yyyy, '-', mm, '-', dd]);
  String _time = formatDate(DateTime.now(), [HH, ':', nn, ':', ss]);
  Widget build(BuildContext context){
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        //日期
        InkWell(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(_date),
              Icon(Icons.arrow_drop_down)
            ],
          ),
          onTap: () {
            DatePicker.showDatePicker(context,
                showTitleActions: true,
                minTime: DateTime.now(),
                maxTime: DateTime(2500, 6, 7),
                theme: DatePickerTheme(
                    headerColor: Colors.white38,
                    backgroundColor: Colors.yellow[50],
                    itemStyle: TextStyle(
                        color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18),
                    doneStyle: TextStyle(color: Colors.black, fontSize: 16)),
                onChanged: (date) {
                  print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                },
                onConfirm: (date) {
                  setState(() {
                    _date=formatDate(date, [yyyy, '-', mm, '-', dd]);
                  });
                }, currentTime: DateTime.now(), locale: LocaleType.en);
          },
        ),
        SizedBox(height: 10),
        //时间
        InkWell(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(_time),
              Icon(Icons.arrow_drop_down)
            ],
          ),
          onTap: () {
            DatePicker.showTimePicker(context,
                showTitleActions: true,
                theme: DatePickerTheme(
                    headerColor: Colors.white38,
                    backgroundColor: Colors.yellow[50],
                    itemStyle: TextStyle(
                        color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18),
                    doneStyle: TextStyle(color: Colors.black, fontSize: 16)),
                onChanged: (date) {
                  print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                },
                onConfirm: (date) {
                  setState(() {
                    _time=formatDate(date, [HH, ':', nn, ':', ss]);
                  });
                }, currentTime: DateTime.now(), locale: LocaleType.en);
          },
        ),
        RaisedButton(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10)
            ),
            child: Text('确定'),
            textColor: Colors.black,
            color: Colors.green[100],
            padding: EdgeInsets.all(10),
            onPressed:(){Navigator.push(context, new MaterialPageRoute(builder: (context) => LetterPaperPage(widget.token)));}
        ),
      ],
    );
  }
}